//
//  UnitDemoTests.m
//  UnitDemoTests
//
//  Created by wangqinglu on 15/11/9.
//  Copyright © 2015年 xs. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "UDLoginManager.h"

@interface UnitDemoTests : XCTestCase {
    int g_countNotification;
    CFRunLoopRef g_currentLoop;
}

@end

@implementation UnitDemoTests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(receivedSignInSuccessNotification:)
     name:UDLOGINMANAGER_NOTIFICATION_SIGNIN_SUCCESS object:nil];
}

- (void)tearDown {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testExample {
//    XCTAssert(NO, @"Fail is a good start");
}

//- (void)testPerformanceExample {
//    // This is an example of a performance test case.
//    [self measureBlock:^{
//        // Put the code you want to measure the time of here.
//    }];
//}

-(void) testLoginManager_sharedInstance {
    UDLoginManager* sut1 = [UDLoginManager sharedInstance];
    UDLoginManager* sut2 = [UDLoginManager sharedInstance];
    
    XCTAssertNotNil(sut1, @"返回实例不能是空值");
    XCTAssertEqualObjects(sut1, sut2, @"单例多次引用，必须返回同一个实例");
}

-(void) testLoginManager_signInWhenNoLogged {
    UDLoginManager* sut = [UDLoginManager sharedInstance];
    [sut logout];
    XCTAssertFalse(sut.isLogged, @"确保登录对象状态为未登录");
    g_countNotification = 0;
    [sut signInWithUserName:@"wql" Password:@"123456"];
    g_currentLoop = CFRunLoopGetCurrent();
    CFRunLoopRun();
    XCTAssertTrue(sut.isLogged, @"正常登录状态");
    XCTAssert(g_countNotification == 1, @"成功通知有且只有一次");
}

-(void) receivedSignInSuccessNotification:(NSNotification*)notification {
    g_countNotification++;
    CFRunLoopStop(g_currentLoop);
}
@end
